﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SmartCollector.Domain.Repository;
using SmartCollector.Domain.Entity;
using SmartCollector.Domain.Exceptions;

namespace SmartCollector.Controls
{
    public partial class AddCategory : Form
    {
        #region Fields
        private CategoryRepository m_categoryRepository;
        private int m_cateId;
        private tbl_Category m_selectedCategory;
        #endregion Fields

        #region Properties
        #endregion Properties

        #region Constructors
        public AddCategory()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AddCategory"/> class.
        /// </summary>
        /// <param name="cateRepository">The cate repository.</param>
        public AddCategory(CategoryRepository cateRepository)
            : this()
        {
            m_categoryRepository = cateRepository;
            LoadParentCategory();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AddCategory"/> class.
        /// </summary>
        /// <param name="cateRepository">The cate repository.</param>
        /// <param name="mode">The mode.</param>
        public AddCategory(CategoryRepository cateRepository, int cateId)
            : this()
        {
            m_categoryRepository = cateRepository;
            m_cateId = cateId;
            m_selectedCategory = cateRepository.Categories
                .Where(c => c.ID == cateId).FirstOrDefault();
            
            LoadParentCategory();
            if (m_selectedCategory != null)
            {
                m_txtCategoryName.Text = m_selectedCategory.CategoryName;
                m_ckbActive.Checked = m_selectedCategory.Active.HasValue && m_selectedCategory.Active.Value;
            }
        }
        #endregion Constructors

        #region Methods

        #region Public Methods
        #endregion Public Methods

        #region Private Methods
        /// <summary>
        /// Loads the parent category.
        /// </summary>
        private void LoadParentCategory()
        {

            List<tbl_Category> parentCategories = m_categoryRepository.Categories
                .Where(c => !c.ParentID.HasValue).ToList();
            parentCategories.Insert(0,
                new tbl_Category
                {
                    CategoryName = "Root",
                    ID = 0,
                    Active = true
                });
            m_cbmParentCategory.DataSource = parentCategories;
            m_cbmParentCategory.DisplayMember = "CategoryName";
        }

        /// <summary>
        /// Called when [save].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void OnSave(object sender, EventArgs e)
        {
            string cateName = m_txtCategoryName.Text.Trim();
            if (cateName.Length > 0)
            {
                if (cateName.Length > 150)
                {
                    MessageBox.Show("Category Name lengh is not greater than 150");
                    return;
                }
                else
                {
                    if (m_selectedCategory == null)
                    {
                        tbl_Category newCate = new tbl_Category
                        {
                            CategoryName = cateName,
                            Active = m_ckbActive.Checked
                        };
                        tbl_Category parentCate = (tbl_Category)m_cbmParentCategory.SelectedItem;
                        if (parentCate.ID != 0)
                        {
                            newCate.ParentID = parentCate.ID;
                        }
                        try
                        {
                            m_categoryRepository.AddNewCategory(newCate);
                            m_categoryRepository.SubmitChanged();
                            m_categoryRepository = null;
                            DialogResult = System.Windows.Forms.DialogResult.OK;
                            this.Close();
                        }
                        catch (CategoryAlreadyExistException ex)
                        {
                            MessageBox.Show(string.Format("Category [{0} with Id {1}] has already exist", ex.CategoryName, ex.CategoryId));
                        }
                    }
                    else
                    {
                        m_selectedCategory.CategoryName = cateName;
                        m_selectedCategory.Active = m_ckbActive.Checked;
                        tbl_Category parentCate = (tbl_Category)m_cbmParentCategory.SelectedItem;
                        if(parentCate.ID!=0){
                            m_selectedCategory.ParentID = parentCate.ID;
                        }
                        else
                        {
                            m_selectedCategory.ParentID = null;
                        }
                        m_categoryRepository.SubmitChanged();
                        DialogResult = System.Windows.Forms.DialogResult.OK;
                        this.Close();
                    }
                    
                }
            }
        }

        /// <summary>
        /// Called when [cancel].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void OnCancel(object sender, EventArgs e)
        {
            this.Close();
            m_categoryRepository = null;
        }
        #endregion Private Methods

        #endregion Methods
    }
}
